import uuid
from datetime import datetime

from myApp.model.db import db


# create table if not exists chatweb.build_model_log
# (
#     id          varchar(100)                       null,
#     `sql_str`       varchar(600)                       null,
#     question    text                               null comment '问题',
#     create_time datetime default CURRENT_TIMESTAMP null
# )
#     comment '数据建模日志';
#


class BuildInfo(db.Model):
    __tablename__ = 'build_model_log'

    id = db.Column(db.String(100), primary_key=True)
    sql_str = db.Column(db.String(600))
    question = db.Column(db.Text)
    create_time = db.Column(db.DateTime, default=datetime.now)

    def serialize(self):
        return {
            'id': self.id,
            'sql_str': self.sql_str,
            'question': self.question,
            'create_time': self.create_time
        }

    @staticmethod
    def create_build(sql_str, question):
        # 生成uuid
        id = str(uuid.uuid1())
        build = BuildInfo(id=id, sql_str=sql_str, question=question)
        db.session.add(build)
        db.session.commit()
        return id

    @staticmethod
    def get_build_by_id(id: str) -> 'BuildInfo':
        build = BuildInfo.query.filter_by(id=id).first()
        return build
